<template>
	<view>
		<button type="default" @click="selectEnterpriseContact">选人</button>
		<button type="default" @click="scanQRCode">扫码</button>
		<button type="default" @click="searchBeacons">扫描蓝牙</button>
		<button type="default" @click="getBeacons">获取蓝牙扫描结果</button>
		<view class="beacons-status" v-for="item in beaconsStatus">{{item}}</view>
		<view class="beacons-list">
			<view class="beacons-item" v-for="item in beacons" style="margin-bottom: 20rpx;">
				<view>major:{{item.major}}</view>
				<view>minor:{{item.minor}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				beaconsStatus: [],
				beacons: []
			}
		},
		methods: {
			selectEnterpriseContact() {
				let that = this
				this.INITWXJSDK(this.CONFIG.appid, location.origin + location.pathname + location.search, function() {
					that.$wx.invoke("selectEnterpriseContact", {
						"fromDepartmentId": -1, // 必填，-1表示打开的通讯录从自己所在部门开始展示, 0表示从最上层开始
						"mode": "single", // 必填，选择模式，single表示单选，multi表示多选
						"type": ["department"], // 必填，选择限制类型，指定department、user中的一个或者多个
						"selectedDepartmentIds": [], // 非必填，已选部门ID列表
						"selectedUserIds": [] // 非必填，已选用户ID列表
					}, function(res) {
						if (res.err_msg == "selectEnterpriseContact:ok") {
							console.log(res.result);
						}
					});
				})

			},
			scanQRCode() {
				let that = this
				this.INITWXJSDK(this.CONFIG.appid, location.origin + location.pathname + location.search, function() {
					that.$wx.scanQRCode({
						desc: 'scanQRCode desc',
						needResult: 0, // 默认为0，扫描结果由企业微信处理，1则直接返回扫描结果，
						scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是条形码（一维码），默认二者都有
						success: function(res) {
							// 回调
						},
						error: function(res) {
							if (res.errMsg.indexOf('function_not_exist') > 0) {
								alert('版本过低请升级')
							}
						}
					});
				})
			},
			searchBeacons() {
				this.INITWXJSDK(this.CONFIG.appid, location.origin + location.pathname + location.search, () => {
					this.beaconsStatus.push("初始化jsdk成功")
					this.$wx.startBeaconDiscovery({
						uuids: ["FDA50693-A4E2-4FB1-AFCF-C6EB07647825"],
						success(res) {
							this.beaconsStatus.push("startBeaconDiscovery成功")
							this.$wx.onBeaconUpdate((beacons) => {
								this.beaconsStatus.push("onBeaconUpdate触发")
								this.beacons = beacons
							})
						},
						fail(errMsg) {
							this.beaconsStatus.push("startBeaconDiscovery失败：" + errMsg)
						}
					});
				})
			},
			getBeacons() {
				this.INITWXJSDK(this.CONFIG.appid, location.origin + location.pathname + location.search, () => {
					this.$wx.getBeacons({
						success(beacons) {
							this.beacons = beacons
						}
					});
				})
			}

		},
		onLoad() {
			this.LOGINACTION()
		}
	}
</script>

<style>
</style>
